
//
//  UIBarButton+Extension.swift
//  Swift01
//
//  Created by Miaoyang on 16/4/13.
//  Copyright © 2016年 Miaoyang. All rights reserved.
//

import UIKit
extension UIBarButtonItem{
    //创建一个带按钮的UIBarButtonItem
    //在分类中只能使用便利构造函数,便利函数最终会调用一个指定构造函数
    convenience init(imageName: String,target:AnyObject?,action: Selector) {
        //创建按
        let button = UIButton()
        //设置按钮属性
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        //添加点击事件
        button.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
        button.sizeToFit()
        //创建UIBarButtonItem对象设置自定义属性
        self.init(customView:button)
        
    }
    //使用类方法
    class func creatBarbuttonItem(imageName:String)->UIBarButtonItem{
        
        //创建按钮
        let button = UIButton()
        //设置按钮图片
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        
        //自动填充
        button.sizeToFit()
        //创建UIBarButton的自定义属性.返回
        return UIBarButtonItem(customView:button)
        
    }
}
